package com.storage.mongo;

import java.net.UnknownHostException;
import java.util.List;

import com.google.code.morphia.query.Query;
import com.mongodb.MongoException;
import com.wink.charity.Charity;

public class DBInterface {
	
	DiscussionForum df;
	CharityForum cf;
	Login login;
	Publish publish;
	
	public DBInterface() throws UnknownHostException, MongoException
	{
		df = new DiscussionForum();
		cf= new CharityForum();
		login = new Login();
		publish = new Publish();
	}
	
	//discussion forum interface methods
	
	public void createTopic(String topicTitle,String topicContent,String createdUser,String communityID) throws UnknownHostException, MongoException
	{	
		//sample data
		//df.createTopic("China news","Post some news in india","prk","ABPM");
		df.createTopic(topicTitle,topicContent,createdUser,communityID);
	}
	
	public void addComments(String topicTitle,String userName,String comments) throws UnknownHostException, MongoException
	{	
		//sample data
		//df.addComments("China News","babu","comments babu");
		df.addComments(topicTitle,userName,comments);
	}
	
	
	public Query<ETopic> getComments(String topicTitle)
	{
		Query<ETopic> query = df.getComments(topicTitle);
		return query;
	}

	public Query<ETopic> getTopics()
	{
		Query<ETopic> query = df.getTopics();
		System.out.println("main fun title111");
		for(ETopic t : query)
		{
			System.out.println("main fun title"+ t.getTopicTitle());
		}
		return query;
	}
	
	
	//charity interface methods
	
	public  void createCharity(String charityName,String charityDescription,String createdUser,String amount, String communityID) throws UnknownHostException, MongoException 
	{
		cf.createCharity(charityName, charityDescription, createdUser,amount, communityID);
	}
	
	public  int donateCharity(String charityID,int donatedAmount,String donatedBy,String communityID) throws UnknownHostException, MongoException
	{
		int returnvalue=0;
		List<Echarity> list = cf.getParticularCharity(charityID);
		for(Echarity c : list)
		{
			cf.donateCharity(c.getCharityName(), donatedAmount, donatedBy, communityID);
			returnvalue=1;
		}
		return returnvalue;
		
		
	}
	public  Query<Echarity> getCharityDetails(String charityName)
	{
		
		Query<Echarity> query = cf.getCharityDetails(charityName);
		
		return query;
	}
	
	public  List<Echarity> getParticularCharity(String charityID)
	{
		
		List<Echarity> list = cf.getParticularCharity(charityID);
		
		return list;
	}
	
	public List<Echarity> listCharity()
	{
		List<Echarity> list = cf.listCharity();
		
		Charity charity = new Charity();

		for(Echarity c : list)
		{
			int amount = getTotalDonation(c.getCharityName());
			c.setTotalAmount(Integer.toString(amount));
		}
		
		return list;
	}
	
	public int getTotalDonation(String charityName)
	{
		int totalAmount = cf.getTotalDonation(charityName);
		return totalAmount;

	}
	
	public String createLogin(String name, String password) throws UnknownHostException, MongoException
	{
		
		String returnValue = login.createLogin(name,password);
		return returnValue;
	
	}
	
	public String createPublish(String server, String port) throws UnknownHostException, MongoException
	{
		
		String returnValue = publish.createPublish(server,port);
		
		return returnValue;
	
	}
	
	public String[] getGuestCommunities()
	{
		String[] communities = null; //{"server1:8899", "server2:9988"};
		return communities;
	}
	
	public void deleteData() throws UnknownHostException, MongoException
	{
		cf.cleardatabase();
	}
}
